package cn.hesion.dataStructures.doubleLinkedList;


public class DoubleLinkedList {
    private HeroNodeDNode head = new HeroNodeDNode(0, "");

    public HeroNodeDNode getHead() {
        return head;
    }


    public void add(HeroNodeDNode node) {
        if (node == null) {
            return;
        }
        if (head.next == null) {
            head.next = node;
            node.pre = head;
            return;
        }
        HeroNodeDNode temp = head.next;
        while (true) {
            // 表明到达找到最后的节点
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //此时已经是最后的节点了；
        temp.next = node;
        node.pre = temp;
    }

    public void delNode(int no) {
        if (head.next == null) {
            return;
        }
        boolean flag;
        flag = false;
        HeroNodeDNode temp = head.next;
        while (temp != null) {
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next = temp.next;
            if (temp.next != null) {
                temp.next.pre = temp.pre;
            }
        }
        return;

    }

    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
        }
        HeroNodeDNode temp = head.next;
        while (true) {
            if (temp.next == null) {
                // 这一步的目的是找到尾节点
                System.out.println(temp);
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }



}
